<template>
  <!-- 新增部门的弹层 -->
  <el-dialog :title="formData.id?'编辑部门':'新增部门'" :visible="showDialog" :before-close="cancel">
    <!-- 表单组件  el-form   label-width设置label的宽度   -->
    <!-- 匿名插槽 -->
    <el-form ref="myForm" label-width="120px" :model="formData" :rules="rules">
      <el-form-item label="部门名称" prop="name">
        <el-input v-model="formData.name" style="width:80%" placeholder="1-10个字符" />
      </el-form-item>
      <el-form-item label="部门编码" prop="code">
        <el-input v-model="formData.code" style="width:80%" placeholder="1-10个字符" />
      </el-form-item>
      <el-form-item label="部门负责人" prop="manager">
        <el-select v-model="formData.manager" style="width:80%" placeholder="请选择">
          <el-option
            v-for="item in emlpoyessList"
            :key="item.id"
            :label="item.username"
            :value="item.username"
          /></el-select>
      </el-form-item>
      <el-form-item label="部门介绍" prop="introduce">
        <el-input v-model="formData.introduce" style="width:80%" placeholder="1-300个字符" type="textarea" :rows="3" />
      </el-form-item>
    </el-form>
    <!-- el-dialog有专门放置底部操作栏的 插槽  具名插槽 -->
    <el-row slot="footer" type="flex" justify="center">
      <!-- 列被分为24 -->
      <el-col :span="6">
        <el-button type="primary" size="small" @click.native="comfirm">确定</el-button>
        <el-button size="small" @click.native="cancel">取消</el-button>
      </el-col>
    </el-row>
  </el-dialog>
</template>

<script>
import { getDepartmentList, addDepartment, getDepartmentDateils, editDepartmentDateils } from '@/api/department'
import { getEmlpoyessList } from '@/api/employees'
export default {
  props: {
    showDialog: {
      type: Boolean,
      default: false
    },
    data: {
      type: Object,
      required: true
    }
  },
  data() {
    const repeatName = async(rule, val, cb) => {
      let isOk
      const { depts } = await getDepartmentList()
      if (this.formData.id) {
        // 编辑
        isOk = depts
          .filter(item => item.pid === this.data.id)
          .some(item => item.name === val && item.id !== this.data.id)
      } else {
        // 新增
        isOk = depts
          .filter(item => item.pid === this.data.id)
          .some(item => item.name === val)
      }
      isOk ? cb(new Error('部门名称不能重复')) : cb()
    }
    const repatCode = async(rule, val, cb) => {
      const { depts } = await getDepartmentList()
      let isOk
      if (this.formData.id) {
        // 编辑
        isOk = depts.some(item => item.code === val && item.id !== this.data.id)
        isOk ? cb(new Error('部门编码不能重复')) : cb()
      } else {
        // 新增
        isOk = depts.some(item => item.code === val)
        isOk ? cb(new Error('部门编码不能重复')) : cb()
      }
    }
    return {
      formData: {
        name: '', // 部门名称
        code: '', // 部门编码
        manager: '', // 部门管理者
        introduce: '' // 部门介绍
      },
      rules: {
        name: [
          { required: true, message: '部门名称不能为空', trigger: 'blur' },
          { required: true, max: 10, message: '部门名称要求1-10个字符', trigger: 'blur' },
          { validator: repeatName, trigger: 'blur' }
        ],
        code: [
          { required: true, message: '部门编码不能为空', trigger: 'blur' },
          { required: true, max: 10, message: '部门编码要求1-10个字符', trigger: 'blur' },
          { validator: repatCode, trigger: 'blur' }
        ],
        manager: [
          { required: true, message: '部门负责人不能为空', trigger: 'blur' }],
        introduce: [
          { required: true, message: '部门介绍不能为空', trigger: 'blur' },
          { trigger: 'blur', required: true, max: 300, message: '部门介绍要求1-50个字符' }
        ]
      },
      emlpoyessList: []
    }
  },
  async created() {
    this.emlpoyessList = await getEmlpoyessList()
  },
  methods: {
    // 确定
    async comfirm() {
      // 校验表单
      // this.$refs.myForm.validator()
      if (this.formData.id) {
        // 编辑
        await editDepartmentDateils(this.formData)
      } else {
        // 新增
        await addDepartment({ ...this.formData, pid: this.data.id })
      }
      this.$emit('comfirm')
      this.formData = {
        name: '', // 部门名称
        code: '', // 部门编码
        manager: '', // 部门管理者
        introduce: '' // 部门介绍
      }
      this.$refs.myForm.resetFields()
    },
    // 取消
    async cancel() {
      await this.$confirm('确定关闭吗')
      this.$emit('cancel')
      this.formData = {
        name: '', // 部门名称
        code: '', // 部门编码
        manager: '', // 部门管理者
        introduce: '' // 部门介绍
      }
      this.$refs.myForm.resetFields()
    },
    // 编辑
    async edit(id) {
      this.formData = await getDepartmentDateils(id)
    }
  }
}
</script>

<style>

</style>
